package com.vict.task.core.impl;

import com.vict.task.bean.dto.LocalTaskSend;
import com.vict.task.bean.entity.LocalTaskExecuteInstance;
import com.vict.task.core.LocalTaskService;
import com.vict.task.dao.LocalTaskExecuteInstanceDao;
import com.vict.task.utils.LocalTaskExecuteUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LocalTaskServiceImpl implements LocalTaskService {

    @Autowired
    LocalTaskExecuteInstanceDao localTaskExecuteInstanceDao;

    @SneakyThrows
    public void commitConsumer(String taskExecuteInstanceID){
        try{

            localTaskExecuteInstanceDao.updateStatusById(taskExecuteInstanceID, LocalTaskExecuteInstance.status_finish);

        }catch(Exception e){
            throw e;
        }
    }

    @SneakyThrows
    @Override
    public void commitConsumer(String taskExecuteInstanceID, List<LocalTaskSend> sendTaskList) {
        try{

            localTaskExecuteInstanceDao.updateStatusById(taskExecuteInstanceID, LocalTaskExecuteInstance.status_finish);

            if(sendTaskList != null && sendTaskList.size() > 0){
                for(LocalTaskSend sendTask : sendTaskList){
                    LocalTaskExecuteUtil.sendTask(sendTask.getType(), sendTask.getT(), sendTask.getActionTime());
                }
            }

        }catch(Exception e){
            throw e;
        }

    }

    @SneakyThrows
    @Override
    public void commitConsumer(String taskExecuteInstanceID, LocalTaskSend sendTask) {
        try{

            localTaskExecuteInstanceDao.updateStatusById(taskExecuteInstanceID, LocalTaskExecuteInstance.status_finish);
            if(sendTask != null){
                LocalTaskExecuteUtil.sendTask(sendTask.getType(), sendTask.getT(), sendTask.getActionTime());
            }

        }catch(Exception e){
            throw e;
        }
    }
}
